/**
 * 公共方法类
 */
import api from '@/core/Api'
class Common {
  /**
   * 取得token
   */
  getToken () {
    let token = window.localStorage.getItem('token')
    console.log(token)
    if (token) {
      return JSON.parse(token).access_token
    }
  }
  clearToken () {
    window.localStorage.removeItem('token')
  }
  /**
   * 登录
   */
  login ({password, username, validateCode, cacheKey, continueLogin}) {
    return api.post({url: window.$env.loginUrl()}, {
      grant_type: 'password',
      auth_type: 'password',
      client_id: window.$env.client_id,
      client_secret: window.$env.client_secret,
      scope: 'login',
      username: username,
      password: password,
      validate_type: 'simple_code',
      validate_code: validateCode,
      cache_key: cacheKey,
      continue_login: continueLogin
    })
  }
  /**
   * 退出
   */
  logout () {
    return api.get({
      url: window.$env.logoutUrl(),
      meta: {
        requiresAuth: true
      }
    })
  }

  repleaceOssHost (url) {
    return url ? url.replace('https://jinssen.oss-cn-shenzhen.aliyuncs.com', 'http://oss.jinssen.com') : ''
  }
}
export default new Common()
